package com.study.jvm.demo.safepoint;

import java.util.concurrent.atomic.AtomicInteger;

public class SafePointTest {

    public static AtomicInteger num = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        test1();
        //test2();
    }

    private static void test1() throws InterruptedException {
        Runnable runnable = () -> {
            for (int i = 0; i < 200000000; i++) {
                num.getAndAdd(1);
            }
        };
        Thread t1 = new Thread(runnable, "测试01线程");
        Thread t2 = new Thread(runnable, "测试02线程");
        t1.start();
        t2.start();
        System.out.println("主线程开始睡觉");
        //记录睡眠时间
        long start = System.currentTimeMillis();
        Thread.sleep(110);

        System.out.println("睡醒了, 一共睡了 : " + (System.currentTimeMillis() - start) + " 毫秒");
        System.out.println("打印一下num 看看结果是多少: " + num);
    }

    private static void test2() throws InterruptedException {
        Runnable runnable = () -> {
            for (long i = 0; i < 200000000; i++) {
                num.getAndAdd(1);
            }
        };
        Thread t1 = new Thread(runnable, "测试01线程");
        Thread t2 = new Thread(runnable, "测试02线程");
        t1.start();
        t2.start();
        System.out.println("主线程开始睡觉");
        //记录睡眠时间
        long start = System.currentTimeMillis();
        Thread.sleep(1000);

        System.out.println("睡醒了, 一共睡了 : " + (System.currentTimeMillis() - start) + " 毫秒");
        System.out.println("打印一下num 看看结果是多少: " + num);
    }
}
